package com.haifeng.im.Controll;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haifeng.im.service.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping(path = "/api/admin")
public class AdminControll {


    @Autowired
    AdminServiceImpl service;

    /**
     * 登录接口
     * @param admin
     * @param password
     * @return
     */
    @GetMapping(path = "login")
    public String login(String admin, String password) {

        if (admin.isEmpty() || password.isEmpty()) {
            return "";
        }
        List<Map> admins = service.findbyadmin(admin, password);

        if (admins.size()!=1){
            return "";
        }else{

            Map ad=admins.get(0);
            if (ad.get("token")==null||ad.get("token").toString().isEmpty()){
                String token= UUID.randomUUID().toString().replaceAll("-","");
                service.updatetoken(admin,token);
                ad.put("token",token);
            }
            return JSON.toJSONString(ad);
        }



    }
}
